package cn.itcast.controller;

import cn.itcast.domain.Company;
import cn.itcast.service.CompanyService;
import cn.itcast.service.impl.CompanyServiceImpl;
import cn.itcast.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 处理企业管理功能
 */
@WebServlet("/store/company/*")
public class CompanyServlet extends BaseServlet {
    //创建业务层对象
    private CompanyService companyService = new CompanyServiceImpl();

    // TODO:2020年7月22日17:06:31 分页查询
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String page =  req.getParameter("page");
        int pageNum = 1;
        if (page!=null && !page.equals("")){
            pageNum = Integer.parseInt(page);
        }

       List<Company> companyList = companyService.findAll(pageNum);
        //分页查询
        PageInfo<Company> pageInfo = new PageInfo<>(companyList);
       //携带数据

        req.setAttribute("page",pageInfo);
       //跳转页面
        req.getRequestDispatcher("/pages/company/list.jsp").forward(req,resp);

    }

    // TODO: 2020年7月22日19:08:11   *****  打开新增页面  *********
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/pages/company/add.jsp").forward(req,resp);
    }


   // TODO: 2020年7月22日19:15:23   ********  新增功能  *********
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //利用beanutils,将前端添加的信息封装成对象
        Company company = BeanUtil.fillBean(req,Company.class,"yyyy-MM-dd");
        //System.out.println(company);
        //调用companyService方法,添加用户
        //System.out.println(company.getExpirationDate()+"----------------------------------");
        companyService.add(company);

        //重新访问数据库,并遍历
        resp.sendRedirect(req.getContextPath()+"/store/company/findAll");
    }

    // TODO: 2020年7月22日20:15:26   ********  数据回显功能  *********
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面参数
        String id = req.getParameter("id");
        //调用service查询
        Company company = companyService.findById(id);
        System.out.println(company);
        //将数据存储到域中
        req.setAttribute("company",company);
        //跳转到update.jsp页面
        req.getRequestDispatcher("/pages/company/update.jsp").forward(req,resp);

    }

    // TODO: 2020年7月22日20:31:01   ********  修改功能  *********
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");

        //修改
        companyService.update(company);

        //重新访问数据库,并遍历
        resp.sendRedirect(req.getContextPath()+"/store/company/findAll");

    }
    // TODO: 2020年7月22日22:29:36   ********  批量删除功能  *********
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端数据
        String ids = req.getParameter("ids");
        //调用service方法删除
        companyService.deleteByIds(ids);

        //重新访问数据库,并遍历
        resp.sendRedirect(req.getContextPath()+"/store/company/findAll");

    }
    // TODO: 2020年7月22日23:23:40   ********  模糊查询  *********
    protected void findLikeName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String page =  req.getParameter("page");
        int pageNum = 1;
        if (page!=null && !page.equals("")){
            pageNum = Integer.parseInt(page);
        }
        String val = req.getParameter("val");
        List<Company> list  = companyService.findLikeName(val,pageNum);
        PageInfo<Company> pageInfo = new PageInfo<Company>(list);
        req.setAttribute("val",val);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/pages/company/list.jsp").forward(req,resp);
    }

}
